package com.yalemang.widget.tableview

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yalemang.widget.tableview.RecyclerViewAdapter
import com.yalemang.widget.tableview.TableAdapter


/**
 * 表格视图
 *
 * @author cl
 */
class TableView: RelativeLayout {

    private lateinit var contentView: View
    private lateinit var tableRecycleView: RecyclerView
    private lateinit var tableAdapter: RecyclerViewAdapter
    var adapter: TableAdapter? = null
    set(value) {
        field = value
        if (value != null) {
            tableAdapter = RecyclerViewAdapter(value)
            tableRecycleView.adapter = tableAdapter
            value.attach(this)
        }
    }

    var lineColor = Color.GRAY
    var lineWidth: Int = 2
    var centerLineColor = Color.GRAY
    var centerLineWidth: Int = 1
    var radius = 8.0f
    // 是否开启表格线
    var isOpenLine = false

    /**
     * 打开表格线
     */
    fun openTableLine(lineColor: Int = Color.GRAY, centerLineWidth: Int = 1, lineWidth: Int = 2, radius: Float = 8.0f) {
        this.lineColor = lineColor
        this.centerLineColor = lineColor
        this.centerLineWidth = centerLineWidth
        this.lineWidth = lineWidth
        this.radius = radius
        isOpenLine = true
        tableAdapter.notifyDataSetChanged()
        val drawable = GradientDrawable()
        drawable.cornerRadius = radius
        drawable.setStroke(lineWidth, lineColor) // 2px边框，颜色为黑色
        drawable.setColor(Color.TRANSPARENT)
        tableRecycleView.background = drawable
    }

    internal fun notifyDataSetChanged() {
        tableAdapter.notifyDataSetChanged()
    }

    fun closeTableLine() {
        isOpenLine = false
        tableAdapter.notifyDataSetChanged()
        tableRecycleView.background = null
    }

    internal var yWidth: Int = 0
    internal var yHeight: Int = 0
    internal var xHeight: Int = 0

    constructor(context: Context) : super(context) {
        initView(null)
    }

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
        initView(attributeSet)
    }

    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        initView(attributeSet)
    }

    private fun initView(attributeSet: AttributeSet?) {
        contentView = LayoutInflater.from(context).inflate(R.layout.view_table, this, true)
        tableRecycleView = contentView.findViewById(R.id.table_recycler_view)
        tableRecycleView.layoutManager = LinearLayoutManager(context)

        // 解决滑动冲突
        tableRecycleView.addOnItemTouchListener(object : RecyclerView.OnItemTouchListener {
            override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
                if (e.action == MotionEvent.ACTION_MOVE) {
                    rv.parent.requestDisallowInterceptTouchEvent(true)
                }
                return false
            }

            override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {

            }

            override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {

            }
        })

        if (attributeSet != null) {
            // 解析自定义属性
            val typedArray: TypedArray = context.obtainStyledAttributes(attributeSet, R.styleable.TableView)

            isOpenLine = typedArray.getBoolean(R.styleable.TableView_isOpenLine, false)
            radius = typedArray.getDimension(R.styleable.TableView_radius, 0.0f)
            if (isOpenLine) {
                val drawable = GradientDrawable()
                drawable.cornerRadius = radius
                drawable.setStroke(lineWidth, lineColor) // 2px边框，颜色为黑色
                drawable.setColor(Color.TRANSPARENT)
                tableRecycleView.background = drawable
            } else {
                tableRecycleView.background = null
            }

            lineWidth = typedArray.getDimension(R.styleable.TableView_lineWidth, 2.0f).toInt()
            centerLineWidth = typedArray.getDimension(R.styleable.TableView_centerLineWidth, 1.0f).toInt()
            xHeight = typedArray.getDimension(R.styleable.TableView_xHeight, LinearLayout.LayoutParams.WRAP_CONTENT.toFloat()).toInt()
            yHeight = typedArray.getDimension(R.styleable.TableView_yHeight, LinearLayout.LayoutParams.WRAP_CONTENT.toFloat()).toInt()
            yWidth = typedArray.getDimension(R.styleable.TableView_yWidth, LinearLayout.LayoutParams.WRAP_CONTENT.toFloat()).toInt()
            lineColor = typedArray.getColor(R.styleable.TableView_lineColor, Color.GRAY)
            centerLineColor = typedArray.getColor(R.styleable.TableView_centerLineColor, Color.GRAY)

            typedArray.recycle()
        }
    }

}